<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   Gtk菜单切换($)*/

!defined('DYHB_PATH') && exit;

// 导入享元接口
Package::importI('IFlyweight','MODE');

class GtkStyleSwitchMenu implements IFlyweight{

	private $_sStyleSettingFile;
	private $_arrStyles=array();
	private $_sDefaultStyleName='';
	private $_sDefaultStyleRcFile='';
	private $_oItemNullStyle;
	private $_bAlert=false;

	public function __construct($sStyleDir,$oMenu=null){
		A::DDIR($sStyleDir);
		$sDir=G::tidyPath($sStyleDir);
		$this->_sStyleSettingFile=$sDir.'.StyleSetting';
		$this->scanStyles_($sDir);
		if(!$oMenu){$this->_oMenu=new GtkMenu();}// 菜单
		else{$this->_oMenu=$oMenu;}
		$this->_oItemNullStyle=new GtkRadioMenuItem(null,G::L('无样式','gtk',null));// 装载菜单 无Gtk樣式
		$this->_oMenu->append($this->_oItemNullStyle);
		$this->_oItemNullStyle->set_active(true);
		$this->_oItemNullStyle->connect('activate', array($this,'onStyleChanging'));
		$this->_oItemNullStyle->set_data('name',G::L('无样式','gtk',null));
		$this->_oItemNullStyle->set_data('rc','');
		foreach($this->_arrStyles as $sStyleName=>&$sRcPath){// 可选样式
			$oItem=new GtkRadioMenuItem($this->_oItemNullStyle,$sStyleName);
			$this->_oMenu->append($oItem);
			$oItem->set_data('name',$sStyleName);
			$oItem->set_data('rc',$sRcPath);
			$oItem->connect('activate', array($this,'onStyleChanging'));// 事件
			if($sStyleName==$this->_sDefaultStyleName){$oItem->set_active(true);}// 默认项
		}
		$this->_oMenu->set_visible(true);
		$this->_bAlert=true;
	}

	public function __destruct(){
		$this->saveSetting_();// 保存 设定
	}

	public function onStyleChanging(GtkMenuItem $aMenuItem){
		if(!$oMenuItem->get_active()){return;}
		$sRcFile=$oMenuItem->get_data('rc');
		$sName=$oMenuItem->get_data('name');
		if(is_file($sRcFile)){GTK::rc_parse($sRcFile);}// 加载樣式
		if($this->_bAlert){DGtk::M(G::L("样式已经切换至：“%s”。但可能要在重新启动应用程序后才会完全生效。",'gtk',null,$sName));}
		$this->_sDefaultStyleName=$sName;// 保存 设定
		$this->_sDefaultStyleRcFile=$sRcFile;
	}

	public function getStyleRc(){
		return $this->_sDefaultStyleRcFile;
	}

	public function getMenu(){
		return $this->_oMenu;
	}

	private function scanStyles_($sStyleDir){
		if(is_file($this->_sStyleSettingFile)and filemtime($this->_sStyleSettingFile)>filemtime($sStyleDir)){// 使用上次的 缓存结果
			$arrSetting=unserialize(String::decode(file_get_contents($this->_sStyleSettingFile)));
			$this->_arrStyles=$arrSetting['arrStyles'];
			$this->_sDefaultStyleName=$arrSetting['sDefaultStyleName'];
			$this->_sDefaultStyleRcFile=$arrSetting['sDefaultStyleRcFile'];
			return;
		}
		else{// 扫描 樣式目录
			$this->_arrStyles=DGtk::findStyle($sStyleDir);
			$sDefaultStyleName='';
			$sDefaultStyleRcFile='';
		}
	}

	private function saveSetting_(){
		$arrSetting['arrStyles']=$this->_arrStyles;
		$arrSetting['sDefaultStyleName']=$this->_sDefaultStyleName;
		$arrSetting['sDefaultStyleRcFile']=$this->_sDefaultStyleRcFile;
		file_put_contents($this->_sStyleSettingFile, String::encode(serialize($arrSetting)));
	}

	static public function getFlyweightKey(){
		list($sStyleDir)= func_get_arg();
		return G::tidyPath($sStyleDir);
	}

}

/** 注册工厂 */
DFlyweight::regInsFactory('GtkStyleSwitchMenu',array('GtkStyleSwitchMenu','__construct'));
